package com.exam.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.exam.dao.ClassDao;
import com.exam.dao.ExamRecordDao;
import com.exam.dao.UserDao;
import com.exam.entity.Classes;
import com.exam.entity.ExamRecord;
import com.exam.entity.User;
import com.exam.entity.UserRecord;
import com.exam.util.PageBean;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
/**
 * 用户的所有操作
 */
@WebServlet("/admin/exam_record")
public class ExamRecordServlet extends BaseServlet {
	ExamRecordDao dao = new ExamRecordDao();
	Gson gson=new Gson();
	// 查一页数据
	protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException, SQLException {
		System.out.println("查一页数据");  // ?m=page&p=4
		String p = request.getParameter("p");  // 你要看的页面
		if (p == null) {
			p="1";
		}
		int intp =  Integer.parseInt(p);  // 根据当前页，算出开始结束的页码
		PageBean pb =new PageBean(); 
		
		List<ExamRecord> examRecordlist=null;
		List<Classes> classlist=dao.sel();
		
		try {
			examRecordlist = dao.sel(intp);
			for (ExamRecord examRecord : examRecordlist) {
				examRecord.setExam_name(dao.selExamName(examRecord.getExam_id()));
				examRecord.setExam_score(dao.selExamScore(examRecord.getExam_id()));
				//查学生人数
				examRecord.setCount(dao.count(examRecord.getRecord_id()));
			}
			//查考试记录数
			int count=dao.count();
			pb.setTotal(count);
			pb.setCurPage(intp);		
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		// 传给JSP  ---》 request
		request.setAttribute("examRecordlist", examRecordlist);
		request.setAttribute("classlist", classlist);
		request.setAttribute("pb", pb);
		request.getRequestDispatcher("/admin/exam_record.jsp").forward(request, response);
	}

	// ID 查一个考试的数据
	protected void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("查一个考试的数据");
		String id = request.getParameter("id");
		int idd=Integer.parseInt(id);//获取recordid
		
		String zongfen = request.getParameter("zongfen");
		String count = request.getParameter("count");
		int count1=Integer.parseInt(count);//获取学生人数
		String p = request.getParameter("p");  // 你要看的页面
		if (p == null) {
			p="1";
		}
		int intp =  Integer.parseInt(p);  // 根据当前页，算出开始结束的页码
		PageBean pb =new PageBean(); 
		
		ExamRecord record=null;
		List<UserRecord> userRecordlist=null;
		List<Classes> classlist=null;
		
		try {
			record = dao.selRecord(idd);  
			userRecordlist= dao.show(intp,record.getRecord_id());
			for (UserRecord userRecord : userRecordlist) {
				userRecord.setUser_name(dao.selUsername(userRecord.getUser_id()));
				userRecord.setClass_name(dao.selClass(userRecord.getClass_id()));
			}
			classlist=dao.selClass();
			pb.setTotal(count1);
			pb.setCurPage(intp);		
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// 传给JSP  ---》 request
		request.setAttribute("classlist", classlist);//班级信息
		request.setAttribute("zongfen", zongfen);//考试总分
		request.setAttribute("record", record);//考试统计信息
		request.setAttribute("userRecordlist", userRecordlist);//考试学生
		request.setAttribute("pb", pb);
		request.getRequestDispatcher("/admin/user_record.jsp").forward(request, response);
	}
	//成绩排名
	protected void ranking(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("classid");
		int classid=Integer.parseInt(id);//获取classid
		String idd = request.getParameter("recordid");
		int recordid=Integer.parseInt(idd);//获取classid
		
		List<UserRecord> userRecordlist=null;
		
		try {
			userRecordlist= dao.grage(recordid,classid);
			for (UserRecord userRecord : userRecordlist) {
				userRecord.setUser_name(dao.selUsername(userRecord.getUser_id()));
				userRecord.setClass_name(dao.selClass(userRecord.getClass_id()));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// 传给JSP  ---》 request
		request.setAttribute("userRecordlist", userRecordlist);//考试学生
		request.getRequestDispatcher("/admin/user_ranking.jsp").forward(request, response);
	}
	//查班级成绩统计
	protected void classRecord(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("classid");
		int classid=Integer.parseInt(id);
		String idd = request.getParameter("recordid");
		int recordid=Integer.parseInt(idd);
		System.out.println(classid+"---"+recordid);
		
		List<UserRecord> userRecordlist=null;
		int[] a=new int[5];
		try {
			userRecordlist= dao.grade(recordid,classid);
			for (UserRecord userRecord : userRecordlist) {
				if (userRecord.getUser_score()>=90) {
					a[0]++;
				}
				else if (userRecord.getUser_score()>=80 && userRecord.getUser_score()<90) {
					a[1]++;
				}
				else if (userRecord.getUser_score()>=70 && userRecord.getUser_score()<80) {
					a[2]++;
				}
				else if (userRecord.getUser_score()>=60 && userRecord.getUser_score()<70) {
					a[3]++;
				}
				else {
					a[4]++;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		PrintWriter out=response.getWriter();
		HashMap<String, Integer> map = new HashMap();
	    map.put("a", a[0]); 
	    map.put("b", a[1]); 
	    map.put("c", a[2]); 
	    map.put("d", a[3]); 
	    map.put("e", a[4]); 
	    String s = gson.toJson(map); 
	    out.print(s);
	    System.out.println(s);
	}
	//给增加考试查试卷
	protected void addExam(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List examlist=null;
        try {
        	examlist = dao.selExam();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("插入失败！。。。");
        }
        //传递考卷名
        req.setAttribute("examlist", examlist);
		req.getRequestDispatcher("/admin/create_record.jsp").forward(req, resp);
	}
	//增加考试
	protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        req.setCharacterEncoding("utf-8");
        
        int flag=0;

        String recordname = req.getParameter("record");
        
        byte sz[]=recordname.getBytes("iso8859-1");
		String record_name = new String(sz,"utf-8");
        
        int exam_id = Integer.parseInt(req.getParameter("exam"));
        String start = req.getParameter("start");
        String end = req.getParameter("end");
        int record_score = Integer.parseInt(req.getParameter("score"));
        
        ExamRecord examRecord=new ExamRecord(exam_id, record_name, start, end, record_score);
        try {
            flag = dao.registerRecord(examRecord);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("插入失败！。。。");
        }

        if (flag==1){
            System.out.println("注册成功");
            resp.sendRedirect("/OnlineExam1/admin/exam_record?m=page");
        }
	}
	//删除record记录
	protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		//获取record_id
		String id = request.getParameter("id");
		System.out.println("record del"+id);
		int recordid=Integer.parseInt(id);
		int b=dao.delUserRecord(recordid);
		int m=dao.delClassRecord(recordid);
		int n=dao.del(recordid);
		System.out.println(n+"--"+m);
		//去哪？？ 去查第一页
		response.sendRedirect("/OnlineExam1/admin/exam_record?m=page");
		
	}
	//删除userrecord记录
	protected void del1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		String id = request.getParameter("id");
		System.out.println("user record del"+id);
		int recordid=Integer.parseInt(id);
		int n=dao.del1(recordid);
		//去哪？？ 去查第一页
		System.out.println(n);
		response.sendRedirect("/OnlineExam1/admin/exam_record?m=page");
		
	}
	
	//增加考试班级
	protected void addClass(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String id = req.getParameter("recordid");
		int recordid=Integer.parseInt(id);
		
		String idd = req.getParameter("classid");
		int classid=Integer.parseInt(idd);
		System.out.println(recordid+"--"+classid);
		dao.addclass(recordid,classid);
		List<User> userlist=dao.sell(classid);
		for (User user : userlist) {
			dao.addUserRecord(user.getUser_id(), recordid, classid);
		}
		
		resp.sendRedirect("/OnlineExam1/admin/exam_record?m=page");
	}

}
